﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using System.IO;
using System.Globalization;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;

namespace JM.JCY.SharePointComponent.UserManagement.UserList
{
    public partial class UserListUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    this.ddlDept.DataSource = proxy.GetAllDepartment();
                    ddlDept.DataTextField = "DeptName";
                    ddlDept.DataValueField = "ID";
                    ddlDept.DataBind();
                }
            }
        }

      

      

        public void SearchStaffProfiles()
        {
            string name = this.txtName.Text.Trim();
            
            int deptId = Int32.Parse(this.ddlDept.SelectedValue.ToString());
         

            DataSetUserManagement.T_UserInfoDataTable staffProfileDT = new DataSetUserManagement.T_UserInfoDataTable();
            using (UserManagementProxy proxy = new UserManagementProxy())
            {
                staffProfileDT = proxy.SearchUserInfo(name, deptId, 0, "0");
            }

          this.gvUserList.DataSource = staffProfileDT;
          this.gvUserList.DataBind();
        }

        protected void imgSearch_Click(object sender, ImageClickEventArgs e)
        {
            SearchStaffProfiles();
        }

        protected void gvUserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvUserList.PageIndex = e.NewPageIndex;
            SearchStaffProfiles();
        }

        protected void gvUserList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lbName = (LinkButton)e.Row.FindControl("lbName");
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                //DataSetUserManagement.T_UserInfoRow userRow = (DataSetUserManagement.T_UserInfoRow)e.Row.DataItem;
                if (lblStatus.Text.Trim() != JCYConstant.StaffProfile.StaffStatus.Active)
                    lbName.ForeColor = System.Drawing.Color.Red;
                int UserID = Convert.ToInt32(((Label)e.Row.FindControl("lblStaffId")).Text.Trim());
                Literal ltDesignation = (Literal)e.Row.FindControl("ltDesignation");

                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    DepartmentTitleDataSet.T_DeptTitleDataTable dt = proxy.GetDeptTitleByUserId(UserID);

                    foreach (DepartmentTitleDataSet.T_DeptTitleRow row in dt.Rows)
                    {
                        ltDesignation.Text = ltDesignation.Text + row["JobTitle"].ToString() + " <img src='/_layouts/JM.JCY.Resource/images/black_square_small.jpg'/>" + row["DeptName"].ToString();
                    }
                    ltDesignation.Text = ltDesignation.Text.Trim().TrimEnd('/');
                }

                Label lblId = (Label)e.Row.FindControl("lblId");
            }
        }

      
    }
}

 
